package com.app.common;
/**
 * 计算距离类
 */
public class CalculateDistance {
    public static final double R = 6371; // 地球半径，单位：公里
    public static Double haversine(Double lat1, Double lon1, Double lat2, Double lon2) {
        try{
            Double dLat = Math.toRadians(lat2 - lat1);
            Double dLon = Math.toRadians(lon2 - lon1);

            Double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                    + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
                    * Math.sin(dLon / 2) * Math.sin(dLon / 2);

            Double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
            Double distance = R * c;
            distance = Math.pow(distance, 2);
            return Math.sqrt(distance);
        }catch (Exception e)
        {
            return 0.1;
        }

    }
}
